#include "summary_state.h"
#include "application/game_rms.h"

#include "audio/audio.h"
#include "lang/lang.h"
#include "console/console.h"

#include "ui/ui_manager.h"
#include "ui/ui_widget.h"

#include "util/timer.h"				// log
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    

#include <cassert>
using namespace gameflow;


SummaryState::SummaryState(camera::FbCamera*	pCamera, const std::string& BasePanel, const std::string& ExecuteOnEnter ,  const std::string& ExecuteOnLeave ) :
	UIState(pCamera, BasePanel, ExecuteOnEnter, ExecuteOnLeave),
	m_TickSound(0),
	m_Won(true)
{

}

SummaryState::~SummaryState()
{

}

bool	SummaryState::OnEnter()
{
	UIState::OnEnter();
	m_Numbers.resize(0);
	return true;
}

bool	SummaryState::OnExit()
{
	UIState::OnExit();
	GameRMS::Instance()->Save();
	return true;
}

bool	SummaryState::Tick(float dt)
{
	UIState::Tick(dt);
	bool Finished = true;
	for (unsigned int i=0; i<m_Numbers.size(); i++)
	{
		m_Numbers[i].Value.Tick(dt);
		if (!m_Numbers[i].Value.IsFinished())
			Finished = false;

		if (m_Numbers[i].pWidget)
		{
			float	Value = m_Numbers[i].Value.GetCurrentNumber();
            if (m_Numbers[i].IsTime)
				m_Numbers[i].pWidget->SetTextA(lang::Lang::FormatTimeNumber(Value));
			else
				m_Numbers[i].pWidget->SetTextA(lang::Lang::FormatCommaNumber((int)Value));
		}
	}
	return true;
}

void	SummaryState::UpdateHighscore()
{

}


void	SummaryState::SetHighscore(int Index, const std::string& Name, const std::string& Location, int Value)
{
	if (m_Panels.empty()) return;
	ui::UIWidget* pPanel = m_Panels[0];
	assert(pPanel);

	if (Index < 0 || Index >= HighscoreC()) return;

	char	Buffer[256];
	sprintf(Buffer, "Name_%i", Index+1);
	ui::UIWidget* pName = pPanel->GetWidgetByName(Buffer);
	sprintf(Buffer, "Location_%i", Index+1);
	ui::UIWidget* pLocation = pPanel->GetWidgetByName(Buffer);
	sprintf(Buffer, "Score_%i", Index+1);
	ui::UIWidget* pScore = pPanel->GetWidgetByName(Buffer);

	if (Name.empty() && Location.empty())
	{
		if (pName) pName->SetIsVisible(false);
		if (pLocation) pLocation->SetIsVisible(false);
		if (pScore) pScore->SetIsVisible(false);
	} else
	{
		if (pName)
		{
			pName->SetIsVisible(true);
			pName->SetTextA(Name);
		}
		if (pLocation) 
		{
			pLocation->SetIsVisible(true);
			pLocation->SetTextA(Location);
		}
		if (pScore)
		{
			pScore->SetIsVisible(true);
			pScore->SetTextA(lang::Lang::FormatCommaNumber(Value));
		}
		
	}

}
void	SummaryState::HideHighscore()
{
	for (int i=0; i<HighscoreC(); i++)
	{
		SetHighscore(i, "", "", 0);
	}
}


void	SummaryState::StopCountUp()
{
	for (unsigned int i=0; i<m_Numbers.size(); i++)
	{
		m_Numbers[i].Value.SetFinished();
	}
	AUDIO().StopSound(m_TickSound);
	if (m_TickSound != 0)
	{
		AUDIO().PlayEvent("end_summary");
		m_TickSound = 0;
	}
}

void	SummaryState::AddNumber(ui::UIWidget* pWidget, float Value, float From, bool IsTime)
{
	if (!pWidget) return;
	CountNumber	Num;
	Num.IsTime = IsTime;
	Num.pWidget = pWidget;
	Num.Value.Setup(From, (float)Value, NumberTime());
	m_Numbers.push_back(Num);
}

void	SummaryState::AddNumber(const std::string& WidgetName, float Value, float From , bool IsTime)
{
	if (m_Panels.empty()) return;
	ui::UIWidget* pPanel = m_Panels[0];
	assert(pPanel);
	AddNumber(pPanel->GetWidgetByName(WidgetName), Value, From, IsTime);
}
void	SummaryState::SetText(const std::string& WidgetName, const std::string& Text)
{
	if (m_Panels.empty()) return;
	if (m_Panels[0])
	{
		ui::UIWidget* pWidget = m_Panels[0]->GetWidgetByName(WidgetName);
		if (pWidget)
			pWidget->SetTextA(Text);
	}
}


void	SummaryState::OnWon()
{

}

void	SummaryState::OnLost()
{

}

